package com.develop.web.in.service;

import com.develop.configuration.AppContext;
import com.develop.data.loan.model.LoanInstallment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public class LoanInstallmentBudgetHubService implements com.develop.web.in.service.api.LoanInstallmentBudgetService<LoanInstallment, LoanInstallment> {

    //TODO：待优化，是否通过AOP去切换接口持有的实现类实例对象，动态传入标记好像做不到
    //TODO：初始化Hub类时，就创造一个Map，把实例都取出来，给每个实例都打个注解，自动取出来

    public List<LoanInstallment> batchPreview(List<LoanInstallment> items) {
        return items.stream().map(this::preview).collect(Collectors.toList());
    };

    public LoanInstallment preview(LoanInstallment item) {

        //获取标志
        String productionNumber = item.getLoanProductionNumber();
        String businessNumber = item.getLoanBusinessNumber();

        //获取类
        String clazzName = MessageFormat.format("com.develop.web.in.service.{0}{1}LoanInstallmentBudgetService", productionNumber, businessNumber);
        com.develop.web.in.service.api.LoanInstallmentBudgetService<LoanInstallment, LoanInstallment> clazz;

        try {
            clazz = (com.develop.web.in.service.api.LoanInstallmentBudgetService<LoanInstallment, LoanInstallment>) AppContext.getBean(Class.forName(clazzName));
            return clazz.preview(item);

        } catch (ClassNotFoundException e) {
            //throw new RuntimeException(e);
            log.info("ClassNotFoundException {}", clazzName);
            return item;
        }

    };
}
